package com.leetcode.Offer;

import java.util.Stack;

public class JZ06 {
    public int[] reversePrint(ListNode head) {//压栈然后取出

        ListNode tmp = head;
        Stack<Integer> stack = new Stack<>();
        while (tmp != null) {
            stack.push(tmp.val);
            tmp = tmp.next;
        }
        int[] ints = new int[stack.size()];
        int a = 0;
        while (stack.size() > 0) {
            ints[a++] = stack.pop();
        }
        return ints;
    }

    public int[] reversePrint2(ListNode head) {//算出链表长度，然后填入

        ListNode tmp = head;
        int count = 0;
        while (tmp != null) {
            count++;
            tmp = tmp.next;
        }
        tmp = head;
        int[] ints = new int[count];
        count--;
        while (tmp != null) {
            ints[count--] = tmp.val;
            tmp = tmp.next;
        }
        return ints;
    }
}
